class write_csv(object):
    def __init__(self, path = None):
        self.__head = []
        self.__a_line = []
        self.__lines = []
        self.__path = path

    def write_head(self, data):
        if isinstance(data, list):
            self.__head = self.__head + data
        elif isinstance(data, (int, float, str, bool)):
            self.__head.append(data)
        else:
            raise TypeError("Only list/int/float/str/bool are support.")

    def write_line(self, data, end = True):
        if isinstance(data, list):
            self.__a_line = self.__a_line + data
        elif isinstance(data, (int, float, str, bool)):
            self.__a_line.append(data)
        else:
            raise TypeError("Only list/int/float/str/bool are support.")

        if end == True:
            self.__lines.append(self.__a_line)
            self.__a_line = []

    def write_table(self, head, lines):
        self.__head = head
        self.__lines = lines

    def clear_lines(self):
        self.__lines = []

    @staticmethod
    def format_a_line(a_list: list):
        if len(a_list) == 0:
            return ""
        a_line = f"{a_list[0]}"
        for astr in a_list[1:]:
            a_line = a_line + f",{astr}"
        a_line = f"{a_line}\n"
        #print(a_line)
        return a_line

    def save(self, path=None):
        if path != None:
            self.__path = path

        with open(self.__path, 'w') as out_file:
            out_file.write(self.format_a_line(self.__head))
            for iline in self.__lines:
                out_file.write(self.format_a_line(iline))